package com.ipzoe.web.core.exception;

import com.ipzoe.common.constant.HttpStatus;
import com.ipzoe.common.core.domain.AjaxResult;
import com.ipzoe.common.exception.BaseException;
import com.ipzoe.common.exception.CustomException;
import com.ipzoe.common.utils.ServletUtils;
import com.ipzoe.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理器
 *
 * @author ruoyi
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 基础异常
     */
    @ExceptionHandler(BaseException.class)
    public Object baseException(HttpServletRequest request, BaseException e) {
        log.error(e.getMessage(), e);
        if (ServletUtils.isAjaxRequest(request)) {
            return AjaxResult.error(e.getCode(),e.getMessage());
        } else {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("errorMessage", e.getMessage());
            modelAndView.setViewName("error/business");
            return modelAndView;
        }
    }

    /**
     * 业务异常
     */
    @ExceptionHandler(CustomException.class)
    public Object businessException(HttpServletRequest request, CustomException e) {
        log.error(e.getMessage(), e);
        if (ServletUtils.isAjaxRequest(request)) {
            if (StringUtils.isNull(e.getCode())) {
                return AjaxResult.error(e.getCode(), e.getMessage());
            }
            return AjaxResult.error(e.getCode(), e.getMessage());
        } else {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("errorMessage", e.getMessage());
            modelAndView.setViewName("error/business");
            return modelAndView;
        }

    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public Object handlerNoFoundException(HttpServletRequest request,Exception e) {
        log.error(e.getMessage(), e);
        if (ServletUtils.isAjaxRequest(request)) {
            return AjaxResult.error(HttpStatus.NOT_FOUND, "路径不存在，请检查路径是否正确");
        } else {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("errorMessage", e.getMessage());
            modelAndView.setViewName("error/404");
            return modelAndView;
        }

    }

    @ExceptionHandler(Exception.class)
    public Object handleException(HttpServletRequest request,Exception e) {
        log.error(e.getMessage(), e);

        if (ServletUtils.isAjaxRequest(request)) {
            return AjaxResult.error(e.getMessage());
        } else {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("errorMessage", e.getMessage());
            modelAndView.setViewName("error/500");
            return modelAndView;
        }
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(BindException.class)
    public Object validatedBindException(HttpServletRequest request,BindException e) {
        log.error(e.getMessage(), e);
        if (ServletUtils.isAjaxRequest(request)) {
            String message = e.getAllErrors().get(0).getDefaultMessage();
            return AjaxResult.error(400,message);
        } else {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("errorMessage", e.getMessage());
            modelAndView.setViewName("error/business");
            return modelAndView;
        }
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Object validExceptionHandler(HttpServletRequest request,MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);
        if (ServletUtils.isAjaxRequest(request)) {
            String message = e.getBindingResult().getFieldError().getDefaultMessage();
            return AjaxResult.error(400,message);
        } else {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("errorMessage", e.getMessage());
            modelAndView.setViewName("error/business");
            return modelAndView;
        }
    }

}
